Grokking Amazon Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Amazon Easy Problems
Redistribute Characters to Make All Strings Equal (easy)
Solution: Redistribute Characters to Make All Strings Equal
Longest Common Prefix (easy)
Solution: Longest Common Prefix
Make Array Zero by Subtracting Equal Amounts (easy)
Solution: Make Array Zero by Subtracting Equal Amounts
Leaf-Similar Trees (easy)
Solution: Leaf-Similar Trees
Path Crossing (easy)
Solution: Path Crossing
Special Array With X Elements Greater Than or Equal X (easy)
Solution: Special Array With X Elements Greater Than or Equal X
Relative Sort Array (easy)
Solution: Relative Sort Array
Transpose Matrix (easy)
Solution: Transpose Matrix
Find Common Characters (easy)
Solution: Find Common Characters
Fizz Buzz (easy)
Solution: Fizz Buzz
Increasing Order Search Tree (easy)
Solution: Increasing Order Search Tree
Largest Odd Number in String (easy)
Solution: Largest Odd Number in String
Best Time to Buy and Sell (easy)
Solution: Best Time to Buy and Sell Stock
Take Gifts From the Richest Pile(easy)
Solution: Take Gifts From the Richest Pile
Remove Duplicates from Sorted List (easy)
Solution: Remove Duplicates from Sorted List
Amazon Medium Problems
Maximum Length of Subarray With Positive Product (medium)
Solution: Maximum Length of Subarray With Positive Product
Largest Submatrix With Rearrangements (medium)
Solution: Largest Submatrix With Rearrangements
Count the Number of Good Subarrays (medium)
Solution: Count the Number of Good Subarrays
Minimum Suffix Flips (medium)
Solution: Minimum Suffix Flips
Minimum Number of Operations to Make Array Empty (medium)
Solution: Minimum Number of Operations to Make Array Empty
Next Greater Element II (medium)
Solution: Next Greater Element II
Make Sum Divisible by P (medium)
Solution: Make Sum Divisible by P
Rotting Oranges (medium)
Solution: Rotting Oranges
Minimum Deletions to Make Character Frequencies Unique (medium)
Solution: Minimum Deletions to Make Character Frequencies Unique
Minimum Adjacent Swaps to Make a Valid Array (medium)
Solution: Minimum Adjacent Swaps to Make a Valid Array
Reverse Integer (medium)
Solution: Reverse Integer
Maximize Distance to Closest Person (medium)
Solution: Maximize Distance to Closest Person
Split Linked List in Parts (medium)
Solution: Split Linked List in Parts
Maximum Difference Between Node and Ancestor (medium)
Solution: Maximum Difference Between Node and Ancestor
Deepest Leaves Sum (medium)
Solution: Deepest Leaves Sum
The kth Factor of n (medium)
Solution: The kth Factor of n
Coin Change II (medium)
Solution: Coin Change II
Sum of Subarray Minimums (medium)
Solution: Sum of Subarray Minimums
Valid Sudoku (medium)
Solution: Valid Sudoku
Longest Increasing Subsequence (medium)
Solution: Longest Increasing Subsequence
Next Permutation (medium)
Solution: Next Permutation
Subsets (medium)
Solution: Subsets
Combination Sum (medium)
Solution: Combination Sum
Number of Provinces (medium)
Solution: Number of Provinces
Number of Islands (medium)
Solution: Number of Islands
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Number of Valid Subarrays (hard)
Solution: Number of Valid Subarrays
Count Vowels Permutation (hard)
Solution: Count Vowels Permutation
Concatenated Words (hard)
Solution: Concatenated Words
Minimum Replacements to Sort the Array (hard)
Solution: Minimum Replacements to Sort the Array
Count All Valid Pickup and Delivery Options (hard)
Solution: Count All Valid Pickup and Delivery Options
Length of the Longest Valid Substring (hard)
Solution: Length of the Longest Valid Substring
Count Array Pairs Divisible by K (hard)
Solution: Count Array Pairs Divisible by K
Word Ladder (hard)
Solution: Word Ladder
Path with Maximum Sum (hard)
Solution: Path with Maximum Sum
Solution: The kth Factor of n

Problem Statement

Given two integers, n and k, return the kth smallest factor of n. If n has fewer factors than k factors, return -1.

A factor of a number is an integer that can divide that number without leaving a remainder.

Examples

  • Example 1:

    • Input: n = 12, k = 5
    • Expected Output: 6
    • Justification: The factors of 12 are 1, 2, 3, 4, 6, and 12. The fifth smallest factor is 6.
  • Example 2:

    • Input: n = 17, k = 2
    • Expected Output: 17
    • Justification: The factors of 17 are 1 and 17, since 17 is a prime number. The second smallest factor is 17.
  • Example 3:

    • Input: n = 10, k = 5
    • Expected Output: -1
    • Justification: The factors of 10 are 1, 2, and 5. 10 has only 3 factors, so the answer is -1.

Solution

To solve this problem, we will first calculate all the factors of n by iterating through numbers from 1 to n and checking if n is divisible by each of these numbers without a remainder. If a number is a factor, we increase the count of factors found so far. If the current factor is the kth smallest factor, we consider it as the answer.

This method is effective because it guarantees that we examine all potential divisors of n, thereby not missing any factors. Although this method may not be the most efficient for very large numbers due to its linear complexity, it is straightforward and reliable for a wide range of inputs.

Step-by-Step Algorithm

  1. Initialize a counter: Start with a variable factorCount set to 0. This counter will keep track of how many factors of n we have found.

  2. Iterate through potential factors: Loop through numbers i starting from 1 up to n inclusive. Each i represents a potential factor of n.

  3. Check for divisibility: For each i, check if n % i == 0. This checks whether i is a factor of n by seeing if the division leaves no remainder.

  4. Increment the factor counter: If i is a factor of n (i.e., the condition in step 3 is true), increment factorCount by 1.

  5. Check if the current factor is the kth factor: If factorCount equals k at any point, return i. This means we've found the kth factor of n.

  6. Finish the loop without finding the kth factor: If the loop completes and factorCount is less than k, return -1. This indicates that n does not have a kth factor.

Algorithm Walkthrough

Let's consider the Input: n = 12, k = 5

  • Step 1: Initialize factorCount to 0.

  • Step 2: Start iterating from i = 1 to i = 12 (inclusive).

    • When i = 1, 12 % 1 == 0. Factor found. Increment factorCount to 1.
    • When i = 2, 12 % 2 == 0. Factor found. Increment factorCount to 2.
    • When i = 3, 12 % 3 == 0. Factor found. Increment factorCount to 3.
    • When i = 4, 12 % 4 == 0. Factor found. Increment factorCount to 4.
    • When i = 5, 12 % 5 != 0. No action is taken since 5 is not a factor.
    • When i = 6, 12 % 6 == 0. Factor found. Increment factorCount to 5.
  • Step 5: At this point, when i = 6, factorCount equals k (which is 5). This means we've found the 5th factor of 12. Return i, which is 6.

  • The algorithm returns 6 as the output, indicating that the 5th smallest factor of 12 is 6.

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity:

The primary operation in this algorithm involves iterating through all numbers from 1 to n to find all factors of n. This iteration constitutes the bulk of the computational work, leading to a time complexity of O, where n is the input number.

Space Complexity:

The algorithm uses the constant space. So, the space complexity is .

The kth Factor of n (medium)
Coin Change II (medium)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity:

Space Complexity: